/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.server.robo;

import com.bullionxpress.server.application.Server;
import com.bullionxpress.server.application.ServerGlobals;
import com.bullionxpress.server.feed.Feed;
import com.bullionxpress.server.impl.AccountRegistrar;
import com.bullionxpress.server.impl.UserRegistrar;
import com.bullionxpress.shared.model.Account;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.model.LimitOrder;
import com.bullionxpress.shared.model.Order;
import com.bullionxpress.shared.model.OrderType;
import com.bullionxpress.shared.model.User;
import java.util.TimerTask;


public class MarketTask extends TimerTask {

    Order buyOrder = new LimitOrder();
    Order sellOrder = new LimitOrder();
    Instrument instrument = null;

    public MarketTask(Instrument instrument)
    {
        this.instrument = instrument;
    }

    public void run() {
        try {
            long buyUnits = 75;
            long sellUnits = 75;
            String strSpread = Server.getProperty("spread");
            double spread = Double.parseDouble(strSpread);// *

            User u = new User();
            UserRegistrar reg = new UserRegistrar();

            u = reg.get("robot");
            AccountRegistrar ar = new AccountRegistrar();

            Account account = ar.getAccountByOwner(u.getUserID());
            buyOrder.setAccount(account);
            sellOrder.setAccount(account);

            String buyId = buyOrder.getId();
            String sellId = sellOrder.getId();

            try {

                Order bo = null;
                Order so = null;
                try {
                    bo = ServerGlobals.getOrderManager().getOrderById(buyId);
                    so = ServerGlobals.getOrderManager().getOrderById(sellId);
                } catch (Exception ex) {
                }
                if (bo != null && so != null) {
                    double _gp = Feed.getPriceByInstrument(instrument, true);
                    if( _gp > 0 ){
                        double bid = _gp - spread;
                        double ask = _gp + spread;
                        bo.setPrice(bid);
                        so.setPrice(ask);
                    }
                } else {

                    double gp = Feed.getPriceByInstrument(instrument, true);
                    if( gp > 0 ){
                        double bid = gp - spread;
                        double ask = gp + spread;
                        buyOrder.setOrderType(OrderType.BUY);
                        buyOrder.setUnits(buyUnits);
                        buyOrder.setInstrument(instrument);
                        sellOrder.setOrderType(OrderType.SELL);
                        sellOrder.setInstrument(instrument);
                        sellOrder.setUnits(sellUnits);
                        sellOrder.setPrice(ask);
                        buyOrder.setPrice(bid);
                        ServerGlobals.getOrderManager().offer(buyOrder);
                        ServerGlobals.getOrderManager().offer(sellOrder);
                    }
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}